import { AssetManager, Material, native, Node, sys } from "cc";
import { PlatFormType } from "../const/enumConst";
import { AndroidPlatform } from "../platform/androidPlatform";
import { IPlatform } from "../platform/basePlatform";
import { IosPlatform } from "../platform/iosPlatform";
import { WebPlatform } from "../platform/webPlatform";
import { ToolsHelper } from "../toolsHelper";
import { SingletonClass } from "./singletonClass";

/**
 * 平台管理
 */
export class PlatformManager extends SingletonClass<PlatformManager> implements IPlatform {

	private curPlatform: IPlatform;     // 当前平台

	public async onInit() {
		if (sys.isNative) {
			if (sys.os === sys.OS.ANDROID) {
				this.curPlatform = new AndroidPlatform();
			} else if (sys.os === sys.OS.IOS) {
				this.curPlatform = new IosPlatform();
			}
			native.bridge.onNative = this.receiveNativeMsg.bind(this);
		} else {
			this.curPlatform = new WebPlatform();
		}
		this.curPlatform.injectGlobalFun()
	}

	/** 获取当期运行环境 */
	public getPlatform() {
		let type = 0;
		if (sys.isNative) {
			if (sys.os === sys.OS.ANDROID) {
				type = PlatFormType.android;
			} else if (sys.os === sys.OS.IOS) {
				type = PlatFormType.ios;
			}
		} else {
			type = PlatFormType.web
		}
		return type;
	}

	loadGame(subGame: number): Promise<AssetManager.Bundle> {
		return this.curPlatform.loadGame(subGame);
	}

	getUUid(): string {
		return this.curPlatform.getUUid();
	}

	changeOrientation(isPortrait: boolean) {
		return this.curPlatform.changeOrientation(isPortrait);
	}

	savePicture(parentNode: Node): any {
		// return this.curPlatform.savePicture(parentNode);
	}

	shootBackground(parentNode: Node, material: Material, targetNode: Node) {
		return this.curPlatform.shootBackground(parentNode, material, targetNode);

	}

	copyToClipboard(content: string): void {
		return this.curPlatform.copyToClipboard(content);
	}

	async getClipboardContent(callback?: Function): Promise<string> {
		let res = await this.curPlatform.getClipboardContent(callback);
		res = ToolsHelper.matchClipboardContent(res);
		return res;
	}

	getPackageName(): Promise<string> {
		return this.curPlatform.getPackageName();
	}

	getAppVersion(): string {
		return this.curPlatform.getAppVersion();
	}

	checkIsEmulator(): boolean {
		return this.curPlatform && this.curPlatform.checkIsEmulator();
	}

	/** 发送消息 */
	sendNativeMsg(operation: string, args: string) {
		return this.curPlatform.sendNativeMsg(operation, args);
	}

	/** 接受消息 */
	receiveNativeMsg(operation: string, data: string) {
		return this.curPlatform.receiveNativeMsg(operation, data);
	}

	faceBookLogin() {
		return this.curPlatform.faceBookLogin();
	}

	faceBookLoginOut() {
		return this.curPlatform.faceBookLoginOut();
	}

	loginGoogle() {
		return this.curPlatform.loginGoogle();
	}

	faceBookShare(content): boolean {
		// return this.curPlatform.faceBookShare(content);
		return false;
	}

	whatsAppShare(url: string): boolean {
		// let phone = App.user.lobbyInfo.baseinfo.phone;
		// let opUrl = `https://wa.me/?text=${url}`
		// if (phone) {
		//     opUrl = `https://wa.me/${phone}?text=${url}`
		// }
		// ToolsHelper.openUrl(opUrl)
		return this.curPlatform.whatsAppShare(url);
	}

	loginOutGoogle() {
		return this.curPlatform.loginOutGoogle();
	}

	injectGlobalFun() {

	}

	jsCallJava(operationType: number) {
		this.curPlatform.jsCallJava(operationType)
	}

	jsCallIos(): void {

	}
}